import axios from "axios";
import Qs from "qs";
import { Message } from "element-ui";
import router from "../router";

// import { config } from "vue/types/umd";

// 环境的切换
if (process.env.NODE_ENV === "development") {
  axios.defaults.baseURL = "";
} else if (process.env.NODE_ENV === "debug") {
  axios.defaults.baseURL = "";
} else if (process.env.NODE_ENV === "production") {
  axios.defaults.baseURL = "";
}
axios.defaults.timeout = 15000;
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8;multipart/form-data";

// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
  (config) => {
    if (localStorage.getItem("Authorization")) {
      config.headers.Authorization = localStorage.getItem("Authorization");
    }
    return config;
  },
  (error) => {
    router.push("/login");
    return Promise.reject(error);
  }
);

//响应拦截器即异常处理
// let token = localStorage.getItem('Authorization')
axios.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = "错误请求";
          break;
        case 401:
          error.message = "未授权，请重新登录";
          router.push("/Login");
          break;
        case 403:
          error.message = "拒绝访问";
          break;
        case 404:
          error.message = "请求错误,未找到该资源";
          break;
        case 405:
          error.message = "请求方法未允许";
          break;
        case 408:
          error.message = "请求超时";
          break;
        case 500:
          if (window.location.pathname == "/Login") {
            error.message = "账号或密码错误!";
          } else {
            error.message = "服务器端出错";
          }

          break;
        case 501:
          error.message = "网络未实现";
          break;
        case 502:
          error.message = "网络错误";
          break;
        case 503:
          error.message = "服务不可用 您无权限查看";
          break;
        case 504:
          error.message = "网络超时";
          break;
        case 505:
          error.message = "http版本不支持该请求";
          break;
        default:
          error.message = `连接错误${error.response.status}`;
      }
    } else {
      error.message = "连接到服务器失败";
    }

    Message.error(error.message);
    return Promise.resolve(error.response);
  }
);

export function axioed(path, params, method = "GET", headerType = "json") {
  let baseURL = "/";
  let data = {}; // 将参数解析出来进行放置的地方
  // 设置请求头
  if (method == "post") {
    if (headerType == "json") {
      axios.defaults.headers.post["Content-Type"] =
        "application/json;charset=utf-8";
      data = params;
    } else if (headerType == "multipart") {
      axios.defaults.headers.post["Content-Type"] = "multipart/form-data";
    } else {
      axios.defaults.headers.post["Content-Type"] =
        "application/x-www-form-urlencoded";
      data = Qs.stringify(params);
    }
  }

  if (method == "get") {
    if (headerType == !"json") {
      axios.defaults.headers["Content-Type"] =
        "application/x-www-form-urlencoded;charset=utf-8";
    }
    data = Qs.stringify(params);
    path = path + "?" + data;
    data = {};
  }
  return new Promise((resolve, reject) => {
    axios({
      method,
      url: baseURL + path,
      data,
      timeout: 6000,
    })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then((res) => {
        resolve(res.data, res.headers);
        // resolve(.date);
        // return Promise.resolve(res);
      })
      .then(Promise.resolve("res"))
      .catch((err) => {
        reject(err);
      });
  });
}

export function gets(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then((res) => {
        resolve(res);
        // resolve(.date);
        // return Promise.resolve(res);
      })
      .then(Promise.resolve("res"))
      .catch((err) => {
        reject(err);
      });
  });
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function posted(url, param, header) {
  return new Promise((resolve, reject) => {
    axios({
      method: "post",
      url,
      params: param,
      headers: header,
    })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err.data);
      });
  });
}
